package zhongbo.java.air.monitor.listener;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.connection.MessageListener;
import org.springframework.stereotype.Service;
import zhongbo.java.air.monitor.mapper.FlightActivityDailyMapper;
import zhongbo.java.air.monitor.web.websocket.AlertZoneWebSocket;

@Slf4j
@Service
public class RedisSubscribeLogChannelListener implements MessageListener {

    @Autowired
    private AlertZoneWebSocket webSocket;


    @Override
    public void onMessage(Message message, byte[] pattern) {
        String channel = new String(message.getChannel());
        String msgBody = new String(message.getBody());
        log.info(
                "\n\u001B[34m[Redis]\u001B[0m \u001B[36m▶▶\u001B[0m 收到频道 \u001B[33m[{}]\u001B[0m 的消息 \n" +
                        "├─ 消息类型: \u001B[35m{}\u001B[0m \n" +
                        "└─ 内容: \u001B[32m{}\u001B[0m",
                channel,
                msgBody.getClass().getSimpleName(),  // 显示消息类型（如String/JSON）
                msgBody.toString().replace("\n", " ")  // 单行显示（避免日志换行混乱）
        );

        webSocket.broadcast(msgBody); // 频道接受到的消息调用推送方法
    }
}
